﻿using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.Rapor.YeniYapi
{
    public partial class SREtiketleri : DevReport
    {
        DataSet ds;
        int EmptyLabelCount;

        public SREtiketleri()
        {
            InitializeComponent();

            foreach (XRTableCell tc in new XRTableCell[] { x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14 })
                tc.BeforePrint += ReSize; 
        }
        
        public SREtiketleri(DataTable dtEtiket, int emptyLabelCount,int TekrarEtiket)
            : this()
        {
            EmptyLabelCount = emptyLabelCount;
            
            dtEtiket.TableName = "dtEtiket";
            DataTable dt = dtEtiket.Clone();
            dt.TableName = "Table";

            ds = new DataSet();
            ds.Tables.AddRange(new DataTable[] { dtEtiket, dt });
            
            //ds = Session.DataManager.ExecuteDataSet("", Xml);

            //Boş etiket sayısı kadar boş DataRow ekliyoruz
            for (int i = 0; i < EmptyLabelCount; i++)
            {
                DataRow r = ds.Tables["Table"].NewRow();
                ds.Tables["Table"].Rows.InsertAt(r, 0);
            }

            //Tekrar sayısı kadar DataRow çoğaltıryoruz
            foreach (DataRow dr in dtEtiket.Rows)
            {
                for (int i = 0; i < TekrarEtiket; i++)
                {
                    dt.ImportRow(dr);
                }
            }

            this.DataMember = "Table";
            this.DataSource = ds;

        }

        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            EmptyLabelCount--;

            this.DataMember = "Table";
            this.DataSource = ds;

            x0.DataBindings.Add("Text", null, "Table.DosyaNo");
            x1.DataBindings.Add("Text", null, "Table.xProduct");
            x2.DataBindings.Add("Text", null, "Table.Artikel");
            x3.DataBindings.Add("Text", null, "Table.Konstruksiyon");
            x4.DataBindings.Add("Text", null, "Table.xKompozisyon");
            x5.DataBindings.Add("Text", null, "Table.xFinish");
            x6.DataBindings.Add("Text", null, "Table.EnGrubu");
            x7.DataBindings.Add("Text", null, "Table.Sezon");
            x8.DataBindings.Add("Text", null, "Table.Model");
            x9.DataBindings.Add("Text", null, "Table.Yaka");
            x10.DataBindings.Add("Text", null, "Table.Customer");
            x11.DataBindings.Add("Text", null, "Table.Manset");
            x12.DataBindings.Add("Text", null, "Table.xTYaka");
            x13.DataBindings.Add("Text", null, "Table.xTManset");
            x14.DataBindings.Add("Text", null, "Table.xTKusak");

            /*
            string strModelDetay = "";
            foreach (DataColumn dc in ds.Tables[0].Columns)
            {
                string activeColName = dc.ColumnName;
                if (GetCurrentColumnValue(activeColName).ToString() != string.Empty)
                {
                    strModelDetay += activeColName + ";";
                }
            }

            string[] Fields = strModelDetay.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < 7; i++)
            {
                ((XRTableCell)this.FindControl("h" + i.ToString(), false)).WidthF = 96.83F;
                if (Fields.Length > i)
                {
                    if (Fields[i] != "Brand")
                        ((XRTableCell)this.FindControl("h" + i.ToString(), false)).Text = Fields[i];//String.Format("{0,-9}" ,Fields[i])+":";
                    else
                    {
                        ((XRTableCell)this.FindControl("h" + i.ToString(), false)).Text = "";
                        ((XRTableCell)this.FindControl("h" + i.ToString(), false)).WidthF = 5;
                    }
                    ((XRTableCell)this.FindControl("x" + i.ToString(), false)).DataBindings.Add("Text", null, "Table." + Fields[i]);
                }
                else
                {
                    ((XRTableCell)this.FindControl("h" + i.ToString(), false)).Text = "";
                    ((XRTableCell)this.FindControl("x" + i.ToString(), false)).DataBindings.Add("Text", null, "");
                }
            }*/
        }


        private void ReSize(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            //Resize ı düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            XRTableCell label = (XRTableCell)sender;

            float newSize = 5.8F;
            Font defaultFont = new System.Drawing.Font("Calibri", newSize);

            float stringWidth = gr.MeasureString(label.Text, defaultFont).Width;
            float labelWidth = label.HeightF;


            if (labelWidth < stringWidth * factor)
                newSize = defaultFont.Size * label.HeightF / stringWidth / factor;


            label.Font = new Font(defaultFont.FontFamily, newSize);
            gr.Dispose();
        }

        private void tbKumas_BeforePrint_1(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            }
        }
    }
}
